package com.mingrisoft;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mingrisoft.database.DBOpenHelper;

import java.util.Random;

public class seekBackPasswordAcitivity extends Activity implements View.OnClickListener {
    private EditText newPassword, confirmNewPassword, verifyCode;
    private String mPhoneNumber, mVerifyCode;
    private DBOpenHelper dbOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_back_password_acitivity);
        newPassword = (EditText) findViewById(R.id.seekBackPasswordEditText);
        confirmNewPassword = (EditText) findViewById(R.id.confirmNewPasswordEditText);
        verifyCode = (EditText) findViewById(R.id.verifyCodeEditText);
        Button btnGetVerifyCode = (Button) findViewById(R.id.verifyCodeButton);
        Button btnRegisterAndBack = (Button) findViewById(R.id.confirmBackLoginButton);
        mPhoneNumber = getIntent().getStringExtra("phoneNumber");
        newPassword.addTextChangedListener(new HideTextWatcher(newPassword, 6));
        confirmNewPassword.addTextChangedListener(new HideTextWatcher(confirmNewPassword, 6));
        verifyCode.addTextChangedListener(new HideTextWatcher(verifyCode, 6));
        btnGetVerifyCode.setOnClickListener(this);
        btnRegisterAndBack.setOnClickListener(this);
        dbOpenHelper = new DBOpenHelper(seekBackPasswordAcitivity.this, "user.db", null, 1);
    }
    protected void onStart() {
        super.onStart();
    }

    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }
    public void onClick(View view) {
        String passwordFirst, passwordSecond;
        switch (view.getId()) {
            case R.id.verifyCodeButton:
                mVerifyCode = String.format("%06d", new Random().nextInt(999999));
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("请记住验证码");
                builder.setMessage("手机号：" + mPhoneNumber + ",本次验证码是" + mVerifyCode + ",请输入验证码");
                builder.setPositiveButton("好的", null);
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            case R.id.confirmBackLoginButton:
                passwordFirst = newPassword.getText().toString();
                passwordSecond = confirmNewPassword.getText().toString();
                if (passwordFirst.length() < 6) {
                    Toast.makeText(seekBackPasswordAcitivity.this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!passwordFirst.equals(passwordSecond)) {
                    Toast.makeText(seekBackPasswordAcitivity.this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mVerifyCode.equals(verifyCode.getText().toString())) {
                    Toast.makeText(seekBackPasswordAcitivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                    return;
                }
               if( updateData(dbOpenHelper.getReadableDatabase(),mPhoneNumber,passwordSecond)>0)
               {
                   Toast.makeText(seekBackPasswordAcitivity.this, "修改成功", Toast.LENGTH_SHORT).show();
               }
                Intent intent = new Intent();
                intent.putExtra("newPassword", passwordFirst);
                seekBackPasswordAcitivity.this.setResult(3, intent);
                seekBackPasswordAcitivity.this.finish();
                break;
            default:
                break;
        }
    }
    private  long updateData(SQLiteDatabase readableDatabase,String userId,String userPassword){
        ContentValues values=new ContentValues();
        values.put("username",userId);
        values.put("password",userPassword);
        return  readableDatabase.update("user",values,"username=?",new String[]{userId});
    }
    private class HideTextWatcher implements TextWatcher {
        private EditText meditText;
        int maxLength;

        public HideTextWatcher(EditText editObject, int maxLength) {
            this.meditText = editObject;
            this.maxLength = maxLength;
        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        public void afterTextChanged(Editable s) {
            String str = s.toString();
            if (str.length() == maxLength) {
                //隐藏输入法
                ViewUtil.hideOneInputMethod(seekBackPasswordAcitivity.this, meditText);
            }
        }
    }
}

